<template>
    <div v-if="isShow" class="verification">
        <p class="title">请输入提取码</p>
        <input @keydown.enter="define" v-model="myPws" type="text" class="pws">
        <button @click="define">确定</button>
    </div>
    <div v-else class="box">
        <p class="title">
            <span>图片</span>
            <span>到期时间</span>
            <span>下载</span>
        </p>
        <ul >
            <li v-for="(item, index) in renderImg" :key="index">
                <span>
                    <img :class="{ 'activeImg': item.expiration_time < timeStamp }" :src="item.path" alt="">
                </span>
                <span :class="{ 'time': item.expiration_time < timeStamp }">{{ item.expiration_date }}</span>
                <span>
                    <el-icon @click="download(item)" size="16">
                        <Download />
                    </el-icon>
                    <i :class="{ 'overdue': 'hp', 'active': item.expiration_time > timeStamp }">已过期</i>
                </span>
            </li>
        </ul>
        <img v-show="loading" class="loading" src="https://cdn.mockplus.cn/image/2018/03/62f3d715-49a1-4b69-a549-ff17c6d732d0.gif" alt="">
    </div>
</template>
<script setup>
import { ref, onMounted, onUpdated } from 'vue';
import { Download } from '@element-plus/icons-vue';
import * as api from '../../api2'
import { ElMessage } from 'element-plus';
let imgUrl = window.location.href
const url = localStorage.getItem('url')
const isShow = ref(false)
const myPws = ref()
const pwd = ref()
const renderImg = ref([])
const timeStamp = ref()
const loading = ref(false)
//检验密码
const checkPwd = () => {
    api.$checkPwd({ hareLink: imgUrl }).then(res => {
        console.log(res)
        if (res.data.is_pwd == 'NO') {
            isShow.value = false
            getShareInfo()
        } else {
            isShow.value = true
            pwd.value = res.data.pwd
        }
    }).catch(err => {
        console.log(err)
    })
}
//确认密码
const define = () => {
    if (myPws.value == pwd.value) {
        isShow.value = false
        getShareInfo()
    } else {
        ElMessage({
            type: 'error',
            message: "密码错误，请重新输入"
        })
    }
}

//获取分享的图片
const getShareInfo = () => {
    api.$ShareInfo({ hareLink: imgUrl }).then(res => {
        console.log(res)
        renderImg.value = res.data.map(item => {
            item.path = item.path.replaceAll('\\', '/')
            item.path = url + '/TransportImage/' + item.path
            return item
        })
    }).catch(err => {
        console.log(err)
    })
}

//获取当前时间戳
const getTime = () => {
    let nowDate = Date.parse(new Date()) / 1000
    timeStamp.value = nowDate
}

//下载图片
const download = (val) => {
    loading.value = true
    api.$getBinaryFileStream({ shareid: val.share_file_id }).then(res => {
        loading.value = false
        if (val.expiration_time > timeStamp.value && res.data.fileStream) {
            const byteCharacters = atob(res.data.fileStream.replace(/^data:image\/(png|jpeg|jpg);base64,/, ""))
            const byteArray = new Array(byteCharacters.length)
            for (let i = 0; i < byteArray.length; i++) {
                byteArray[i] = byteCharacters.charCodeAt(i)
            }
            const byteUnit8Array = new Uint8Array(byteArray)
            const blob = new Blob([byteUnit8Array], { type: val.suffix })
            const a = document.createElement('a')
            a.href = URL.createObjectURL(blob)
            a.download = `分享图片.${res.data.suffix}`
            a.click()
        } else {
            ElMessage({
                type: 'error',
                message: 'The picture has expired or cannot be found'
            })
        }
    }).catch(err => {
        console.log(err)
    })
}

onMounted(() => {
    checkPwd()
    getTime()
})
</script>
<style lang="less" scoped>
.box {
    position: relative;
    width: 100%;
    height: 100vh;
    .title {
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        width: 100%;
        height: 40px;
        border-bottom: 1px solid #ebeef5;
        padding-left: 40px;
        user-select: none;

        span {
            text-align: center;
            width: 440px;
            font-size: 14px;
            color: #909399;
            font-weight: bold;
            user-select: none;
        }

    }

    ul {
        width: 100%;
        height: 800px;
        overflow-y: scroll;

        li {
            display: flex;
            justify-content: space-evenly;
            align-items: center;
            width: 100%;
            height: 60px;
            font-size: 14px;
            padding-left: 40px;
            margin-top: 20px;
            user-select: none;

            span {
                text-align: center;
                width: 440px;
                height: 60px;
                line-height: 60px;
                user-select: none;
            }

            .time {
                color: #caccd1;
                text-decoration: line-through;
            }

            span:nth-child(1) {
                .activeImg {
                    filter: grayscale(100%);
                }
            }

            span:nth-child(3) {
                position: relative;
                cursor: pointer;
                transform: translateX(20px);

                .overdue {
                    position: absolute;
                    color: red;
                    margin-left: 10px;
                    font-size: 12px;
                    vertical-align: middle;
                }

                .active {
                    display: none;
                }
            }

            img {
                width: 60px;
                height: 60px;
            }
        }

        .activeLi {
            background-color: #f1f1f1f1;
        }
    }
    .loading{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);
    }
}

.verification {
    width: 350px;
    height: 180px;
    border: 1px solid #81a9db;
    border-radius: 5px;
    margin: 0 auto;
    transform: translateY(50%);
    box-shadow: 5px 5px 5px #f1f1f1f1;

    .title {
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
        font-size: 16px;
        color: #909399;
        font-weight: bold;
    }

    .pws {
        display: block;
        width: 250px;
        height: 30px;
        border: 1px solid #909399;
        border-radius: 5px;
        margin: 10px auto;
        outline: none;
        padding-left: 10px;

    }

    button {
        display: block;
        width: 80px;
        height: 30px;
        background-color: #81a9db;
        border: none;
        border-radius: 5px;
        color: #ffffff;
        text-align: center;
        line-height: 30px;
        margin: 30px auto;
        cursor: pointer;

        &:hover {
            opacity: .8;
        }
    }
}
</style>